package com.njth.servlet.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.njth.beans.Categoryfunc;
import com.njth.dao.CategoryfuncDAO;
import com.njth.dao.DaoConfig;
import com.njth.util.Config;
import com.njth.util.FileAssistant;
import com.njth.util.ScaleImage;
/*
 * author :陈帅
 * date   :2008-3-19
 * */
public class AddCategoryFunc extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AddCategoryFunc() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		CategoryfuncDAO categoryfuncdao=(CategoryfuncDAO)DaoConfig.getDao(CategoryfuncDAO.class);
		String path=Config.getConfigProperty("uploadImagePath");
		File uploadAbsolutDirectory =new File(Config.getContenAbsolutPath(),path);
		Map<String,Object> map=FileAssistant.fileUpload(request, uploadAbsolutDirectory);
		String funcname=(String)map.get("categoryfuncname");
		Categoryfunc categoryfunc = new Categoryfunc();
		categoryfunc.setFuncname(funcname);
		File funcimg = (File)map.get("file2");
		String fName = funcimg.getName();
		categoryfunc.setFuncpic(fName);
		
		String[] strs = fName.split("\\.");
        String tName = "";
        for (int j= 0; j < strs.length; j++) {
            if (j == strs.length - 1) {
                tName = tName + "_t." + strs[j];
            } else {
                tName = tName + strs[j];
            }
        }
      
		categoryfunc.setFuncpic(fName);
		ScaleImage si=new ScaleImage();
		try {
			String var=Config.getConfigProperty("imgThumbnailHeight");
			int h=Integer.parseInt(var);
			var=Config.getConfigProperty("imgThumbnailWidth");
			int w=Integer.parseInt(var);
			si.saveImageAsJpg(funcimg.getAbsolutePath(), uploadAbsolutDirectory.getAbsolutePath()+File.separatorChar+tName, w, h);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.setAttribute("newCate", categoryfunc);
		categoryfuncdao.insert(categoryfunc);
		
		
		categoryfuncdao.insert(categoryfunc);
		//产品类别功能插入成功，跳转到指定页面
		out.write("添加成功！");
		response.sendRedirect(request.getContextPath()+"/com/njth/jsp/addCategoryfunc.jsp");
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
